package etnorservice.etnor_service.service.impl;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/11/30/11:31
 **/

import etnorservice.etnor_service.domain.po.Like;
import etnorservice.etnor_service.mapper.LikeMapper;
import etnorservice.etnor_service.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Override
    @Transactional
    public void addLike(Like like) {
        like.setCreateTime(new Date());
        likeMapper.insert(like);
    }

    @Override
    @Transactional
    public void removeLike(Long userId, Long targetId, Integer targetType) {
        likeMapper.delete(userId, targetId, targetType);
    }

    @Override
    public boolean hasUserLiked(Long userId, Long targetId, Integer targetType) {
        return likeMapper.findByUserIdAndTarget(userId, targetId, targetType) != null;
    }

    @Override
    public int getLikeCount(Long targetId, Integer targetType) {
        return likeMapper.countByTarget(targetId, targetType);
    }
}